<template>
	<div>
		<el-row>
			<el-col :span="24">

				<el-form :inline="true" :model="formInline" class="demo-form-inline">
					<el-form-item label="章节名称">
						<el-input size="small" v-model="formInline.name" placeholder="请输入章节名称"></el-input>
					</el-form-item>

					<el-form-item label="章节内容">
						<el-input size="small" v-model="formInline.text" placeholder="请输入章节内容"></el-input>
					</el-form-item>

					<el-form-item>
						<el-button size="small" type="primary" @click="onSubmit">查询</el-button>
					</el-form-item>
				</el-form>


			</el-col>
		</el-row>

		<el-row>
			<el-col :span="24">
				<el-button type="primary" icon="el-icon-plus" size="mini" plain
					@click="dialogFormVisible=true">新增</el-button>
				<el-button type="danger" icon="el-icon-search">搜索</el-button>
				<el-button type="info" icon="el-icon-search">搜索</el-button>

			</el-col>
		</el-row>

		<el-row>
			<el-col :span="24">
				<!-- 
			   1. :data 数据 对象数组 [{}]
			   2.  el-table-column   prop 数据项的名称  label 列名
			   -->
				<el-table :data="tableData" style="width: 100%" @sort-change="sortData">
					<el-table-column prop="id" label="序号" width="180" sortable="custom">
					</el-table-column>
					<el-table-column prop="courseId" label="课程ID" width="180">
					</el-table-column>
					<el-table-column prop="name" label="章节名称" width="180">
					</el-table-column>
					<el-table-column prop="text" label="章节内容" width="180">
					</el-table-column>
					<el-table-column prop="paiXu" label="排序" width="180">
					</el-table-column>
					<el-table-column prop="isShow" label="是否展示" width="180">
					</el-table-column>
					<el-table-column prop="createTime" label="创建时间" width="180">
					</el-table-column>
					<el-table-column prop="updateTime" label="更新时间" width="180">
                    </el-table-column>
					<el-table-column label="操作" width="180">

						<template slot-scope="a">
							<el-button type="primary" icon="el-icon-edit" circle @click="edit(a.row)"></el-button>


							<el-popconfirm @confirm="del(a.row.id)" title="这是一段内容确定删除吗？">
								<!-- <el-button slot="reference">删除</el-button> -->
								<el-button type="danger" icon="el-icon-delete" circle slot="reference"></el-button>
							</el-popconfirm>
						</template>
					</el-table-column>
				</el-table>

			</el-col>
		</el-row>

		<el-row>
			<el-col :span="24" style="display: flex;justify-content: end;">
				<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
					:current-page="formInline.pageNo" :page-sizes="[2,10, 20, 30, 40]" :page-size="formInline.pageSize"
					layout="total, sizes, prev, pager, next, jumper" :total="total">
				</el-pagination>
			</el-col>
		</el-row>


		<el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible">
			<el-form :model="form">
				<el-form-item label="章节名称" :label-width="formLabelWidth">
					<el-input v-model="form.name" autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="章节内容" :label-width="formLabelWidth">
					<el-input v-model="form.text" autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="排序" :label-width="formLabelWidth">
					<el-input v-model="form.paiXu" autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="是否展示" :label-width="formLabelWidth">
					<el-input v-model="form.isShow" autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="课程ID" :label-width="formLabelWidth">
					<el-input v-model="form.courseId" autocomplete="off"></el-input>
				</el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="dialogFormVisible = false">取 消</el-button>
				<el-button type="primary" @click="add">确 定</el-button>
			</div>
		</el-dialog>

	</div>
</template>

<script>
	import axios from 'axios'

	import {
		getCourseChapts,
		addCourseChapts,
		getOne,
		editCourseChapts,
		delCourseChapts
	} from '../api/coursechapts.js'

	export default {
		data() {
			return {
				formInline: {
					name: '',
				    text:'',
					pageNo: 1,
					pageSize: 10,
					sort: "id",
					order: "desc"
				},
				total: 0,
				tableData: [],
				dialogFormVisible: false,
				form: {
					name: '',
					text: '',
					paiXu:'',
					isShow:'',
					courseId:''	
				},
				formLabelWidth: '120px',
				dialogTitle: "新增章节",
				imageUrl: ''
			}
		},
		methods: {
			onSubmit() {
				this.getData();
			},
			del(id) {

				delCourseChapts({
					id
				}).then(res => {
					if (res.data.code == 200) {
						this.$message({
							message: "删除成功",
							type: "success"
						})
					} else {
						this.$message({
							message: res.data.message,
							type: "error"
						})

					}
				})
				this.getData();
			},
			add() {

				if (this.form.id > 0) {
					// 新增
					editCourseChapts(this.form).then(res => {
						if (res.data.code == 200) {
							this.$message({
								message: "编辑成功",
								type: "success"
							})
						} else {
							this.$message({
								message: res.data.message,
								type: "error"
							})

						}
						this.dialogFormVisible = false;

						this.getData();
					})
				} else {
					// 新增
					addCourseChapts(this.form).then(res => {
						if (res.data.code == 200) {
							this.$message({
								message: "新增成功",
								type: "success"
							})
						} else {
							this.$message({
								message: res.data.message,
								type: "error"
							})

						}
						this.dialogFormVisible = false;

						this.getData();
					})
				}


			},
			sortData(res) {
				console.log(res)
				if (res.order == "descending") {
					this.formInline.order = "desc";
				} else {
					this.formInline.order = "asc";
				}
				this.formInline.sort = res.prop;
				this.getData();
			},
			handleCurrentChange(num) {
				this.formInline.pageNo = num;

				this.getData();
			},
			handleSizeChange(size) {
				this.formInline.pageSize = size;

				this.getData();
			},
			edit(coursechapts) {
				this.dialogFormVisible = true;
				this.dialogTitle = "编辑章节";
				//发送请求
				getOne(coursechapts.id).then(res => {
					console.log(res.data)
					this.form = res.data;
				})
			},
			getData() {
				getCourseChapts(this.formInline).then((res) => {
					this.tableData = res.data.data.rows;
					this.total = res.data.data.total;
				})
			}
		},
		mounted() {
			this.getData()

		}
	}
</script>

<style>
	.avatar-uploader .el-upload {
		border: 1px dashed #d9d9d9;
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
	}

	.avatar-uploader .el-upload:hover {
		border-color: #409EFF;
	}

	.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		line-height: 178px;
		text-align: center;
	}

	.avatar {
		width: 178px;
		height: 178px;
		display: block;
	}
</style>